package com.xj.community.controller;

import com.xj.community.Service.CommentService;
import com.xj.community.Service.DiscussPostService;
import com.xj.community.Service.LikeService;
import com.xj.community.Service.UserService;
import com.xj.community.entity.Comment;
import com.xj.community.entity.DiscussPost;
import com.xj.community.entity.Page;
import com.xj.community.entity.User;
import com.xj.community.util.CommunityConstant;
import com.xj.community.util.CommunityUtil;
import com.xj.community.util.MyThreadLocal;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.*;

@Controller
@RequestMapping("/discuss")
public class DiscussPostController implements CommunityConstant {
    @Autowired
    private DiscussPostService discussPostService;

    @Autowired
    private CommentService commentService;

    @Autowired
    private UserService userService;

    @Autowired
    private LikeService likeService;

    @RequestMapping(path = "insert",method = RequestMethod.POST)
    @ResponseBody
    public String insertDiscussPost(String title,String content){
        //只有在登录状态下才能发布帖子
        User user = MyThreadLocal.getUser();
        if(user == null){
            return CommunityUtil.getJSONString(403,"当前还未登录，不能发布帖子！");
        }
        DiscussPost discussPost = new DiscussPost();
        discussPost.setTitle(title);
        discussPost.setContent(content);
        discussPost.setUserId(user.getId());
        discussPost.setCreateTime(new Date());
        discussPostService.insertDiscussPost(discussPost);
        return CommunityUtil.getJSONString(200,"发布成功");
    }


    //查询帖子详细信息(评论信息)
    @RequestMapping(path = "/detail/{discussPostId}", method = RequestMethod.GET)
    public String getDiscussPost(@PathVariable("discussPostId") int discussPostId, Model model, Page page) {
        // 帖子
        DiscussPost post = discussPostService.findDiscussPostById(discussPostId);
        model.addAttribute("post", post);
        // 作者
        User user = userService.selectById(post.getUserId());
        model.addAttribute("user", user);

        // 点赞数量
        long likeCount = likeService.findEntityLikeCount(ENTITY_TYPE_POST, discussPostId);
        model.addAttribute("likeCount", likeCount);
        // 点赞状态
        int likeStatus = MyThreadLocal.getUser() == null ? 0 :
                likeService.findEntityLikeStatus(MyThreadLocal.getUser().getId(), ENTITY_TYPE_POST, discussPostId);
        model.addAttribute("likeStatus", likeStatus);

        // 评论分页信息
        page.setLimit(5);
        page.setPath("/discuss/detail/" + discussPostId);
        page.setRows(post.getCommentCount());

        // 评论: 给帖子的评论
        // 回复: 给评论的评论
        // 评论列表

        //commentList中存储了该条帖子对应的所有评论
        List<Comment> commentList = commentService.findCommentsByEntity(
                ENTITY_TYPE_POST, post.getId(), page.getOffset(), page.getLimit());

        // 评论VO列表,commentVoList中存储了每条评论的详细组成：评论，作者，回复列表
        List<Map<String, Object>> commentVoList = new ArrayList<>();

        if (commentList != null) {
            for (Comment comment : commentList) {
                // 评论VO
                Map<String, Object> commentVo = new HashMap<>();
                // 评论
                commentVo.put("comment", comment);
                // 作者
                commentVo.put("user", userService.selectById(comment.getUserId()));

                // 点赞数量
                likeCount = likeService.findEntityLikeCount(ENTITY_TYPE_POST, discussPostId);
                commentVo.put("likeCount", likeCount);
                // 点赞状态
                likeStatus = MyThreadLocal.getUser() == null ? 0 :
                        likeService.findEntityLikeStatus(MyThreadLocal.getUser().getId(), ENTITY_TYPE_POST, discussPostId);
                commentVo.put("likeStatus", likeStatus);

                // 回复列表
                List<Comment> replyList = commentService.findCommentsByEntity(
                        ENTITY_TYPE_COMMENT, comment.getId(), 0, Integer.MAX_VALUE);
                // 回复VO列表，存储了每一条回复的详细组成
                List<Map<String, Object>> replyVoList = new ArrayList<>();
                if (replyList != null) {
                    for (Comment reply : replyList) {
                        Map<String, Object> replyVo = new HashMap<>();

                        // 点赞数量
                        likeCount = likeService.findEntityLikeCount(ENTITY_TYPE_POST, discussPostId);
                        replyVo.put("likeCount", likeCount);
                        // 点赞状态
                        likeStatus = MyThreadLocal.getUser() == null ? 0 :
                                likeService.findEntityLikeStatus(MyThreadLocal.getUser().getId(), ENTITY_TYPE_POST, discussPostId);
                        replyVo.put("likeStatus", likeStatus);

                        // 回复
                        replyVo.put("reply", reply);
                        // 作者
                        replyVo.put("user", userService.selectById(reply.getUserId()));
                        // 回复目标
                        User target = reply.getTargetId() == 0 ? null : userService.selectById(reply.getTargetId());
                        replyVo.put("target", target);

                        replyVoList.add(replyVo);
                    }
                }
                commentVo.put("replys", replyVoList);

                // 回复数量
                int replyCount = commentService.findCommentCount(ENTITY_TYPE_COMMENT, comment.getId());
                commentVo.put("replyCount", replyCount);

                commentVoList.add(commentVo);
            }
        }

        model.addAttribute("comments", commentVoList);

        return "/site/discuss-detail";
    }
}
